/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.boundary;

import com.sunrise.entity.Calendar;
import com.sunrise.entity.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author frank
 */
@Stateless
public class UserManager {

    @PersistenceContext
    EntityManager entityManager;

    @Inject
    Principal principal;

    public void save(User user) {
        user.setGroupName("USER");
        Calendar calendar = new Calendar("main");
        calendar.setOwner(user);
        user.getCalendars().add(calendar);
        entityManager.persist(user);
    }

    public void update(User user) {
        entityManager.merge(user);
    }

    public void unregister(User user) {
        entityManager.remove(user);
    }

    public User getLoggedUser() {
        String res = principal.getName();
        if (res.equals("ANONYMOUS")) {
            return null;
        } else {
            return findUserByEmail(res);
        }
    }

    public User findUserById(Long id) {
        User result = null;
        
        try {
            result = entityManager.createNamedQuery(User.findById, User.class)
                    .setParameter("id", id)
                    .getSingleResult();
        } catch (Exception ex) {
           
        }
        
        return result;
    }

    public User findUserByEmail(String email) {
        User result = null;
        
        try {
            result = entityManager.createNamedQuery(User.findByEmail, User.class)
                    .setParameter("email", email)
                    .getSingleResult();
        } catch (Exception ex) {
        
        }
        
        return result;
    }

    public List<User> findUserSuggestions(String searchQuery, User loggedUser) {
        List<User> result = new ArrayList<>();
        
        try {
            result = entityManager.createNamedQuery(User.search, User.class)
                .setParameter("query", "%" + searchQuery + "%")
                .setParameter("ownId", loggedUser.getId())
                .setMaxResults(10)
                .getResultList();
        } catch (Exception ex) {
        
        }
        
        return result;
    }

    public List<User> findUserWithPublicCalSuggestions(String searchQuery, User loggedUser) {
        List<User> result = new ArrayList<>();
        
        try {
            result = entityManager.createNamedQuery(User.searchUsersWithPublicCal, User.class)
                .setParameter("query", "%" + searchQuery + "%")
                .setParameter("ownId", loggedUser.getId())
                .setMaxResults(10)
                .getResultList();
        } catch (Exception ex) {
        
        }
        
        return result;
    }
}
